library(RSofia);

run.algorithm <- function(m, train, test){
  train <- train[1:m,];
  err <- function(model, data){
    y <- sign(predict.sofia(model,data,"linear"));
    mean(y!=data$IsBetter)
  }
  f <- IsBetter ~ AnswerTextLength+AnswerTimeSec+
                  AnswerUserReputation+AnswerUserNumMonths;
  model <- sofia(f, train);
  train.err <- err(model, train);
  test.err <- err(model, test);
  
  return(data.frame(m, train.err, test.err));
}

do.test <- function(data.svm){
  qids <- unique(data.svm$qid)
  train.questions <- sample(qids, floor(0.7*length(qids)));
  train <- data.svm[data.svm$qid %in% train.questions,];
  test <- data.svm[!(data.svm$qid %in% train.questions),];
  
   m <- c(250, 500, 1000, 2000, 4000, 8000, 16000, 32000, 64000, 128000, 256000);
  res <- lapply(m, run.algorithm, train, test);
  df <- do.call("rbind", res)
  
  run.algorithm(125, train, test)
}
